/*
通过 mutation 间接更新 state 的多个方法的对象
*/
import dayjs from "dayjs";
// 接口函数
import {
  reqSendInfo,
  reqCameraNum,
  reqTokenNum,
  reqAddUrlInfo,
  reqReloadUrlInfo,
  reqEmptyUrlInfo,
  reqTestItemInfo,
  reqStartTest,
  reqTestResultInfo,
  reqDeviceUsers,
  reqUserPlayCamerNums,
  reqOnlineStreamId,
  reqStreamUrlById,
  reqTaskInfo,
  reqMonitorResultInfo,
  reqCameraStatus,
  reqPushStream,
  reqStopStream,
  reqDeviceUsersInfo,
  reqStreamStart,
  reqStreamFinish,
  reqPtzControl
} from "@/api";
import { resolve } from "core-js/fn/promise";

export default {
  handleAuthenticate({ commit }, token) {
    commit("SET_TOKEN", { token });
    commit("SET_LOGIN_STATUS", true);
    sessionStorage.setItem("token", JSON.stringify(token));
  },

  async getQueryInfo({ commit }, params) {
    commit("SETSENDINPARAMS_OTHER", { params }); //发送请求立即更新sendInParams值
    let resData = await reqSendInfo(params);
    commit("GETQUERYINFO", {
      params,
      resData
    });
  },
  async getQueryInfo_vTBegin({ commit }, params) {
    commit("SETSENDINPARAMS", { params }); //发送请求立即更新sendInParams值
    let resData = await reqStreamStart(params.sendParams, params.sendCompany);
    commit("GETQUERYINFO_VTBEGIN", {
      params,
      resData
    });
  },
  async getQueryInfo_vTFinish({ commit }, params) {
    commit("SETSENDINPARAMS", { params }); //发送请求立即更新sendInParams值
    let resData = await reqStreamFinish(params.sendParams, params.sendCompany);
    commit("GETQUERYINFO_VTFINISH", {
      params,
      resData
    });
  },
  async getQueryInfo_ptzControl({ commit }, params) {
    commit("SETSENDINPARAMS", { params }); //发送请求立即更新sendInParams值
    let resData = await reqPtzControl(params.sendParams, params.sendCompany);
    commit("GETQUERYINFO_PTZCONTROL", {
      params,
      resData
    });
  },

  async getCameraNum({ commit }, param) {
    let resData = await reqCameraNum(param);
    commit("GETCAMERANUM", { resData });
  },

  async getTokenNum({ commit }) {
    let resData = await reqTokenNum();
    commit("GETTOKENNUM", { resData });
  },

  //获得厂商列表
  async getDeviceUsers({ commit }) {
    let resData = await reqDeviceUsers();
    commit("GETDEVICEUSERS", { resData });
  },

  //获得厂商的cameraNums列表
  async getUserPlayCamerNums({ commit }, params) {
    let resData = await reqUserPlayCamerNums(params);
    commit("GETUSERPLAYERCAMERNUMS", { resData });
  },

  //获取厂商online的流id
  async getOnlineStreamId({ commit }, params) {
    let resData = await reqOnlineStreamId(params);
    commit("GETONLINESTREAMID", { resData });
  },

  //根据online的流id获取对应url
  async getStreamUrlById({ commit }, params) {
    let resData = await reqStreamUrlById(params);
    commit("GETSTREAMURLBYID", { resData, params });
  },

  async changeTokenStateVal({ commit }, params) {
    // let resData = await reqTokenNum();
    commit("CHANGETOKENSTATEVAL", { params });
  },

  //插入流媒体地址
  async addUrlInfo({ commit }, params) {
    console.log("请求发送信息:", params);
    // let param1 = params.sendParams;
    // let param2 = params.json_data;
    let resData = await reqAddUrlInfo(params);
    // async getQueryInfo({ commit }, params) {
    //let resData = await reqSendInfo(params);
    commit("ADDURLINFO", { resData });
  },
  //重载测试用例
  async reloadUrlInfo({ commit }) {
    let resData = await reqReloadUrlInfo();
    commit("RELOADURLINFO", { resData });
  },
  //清空测试用例
  async emptyUrlInfo({ commit }) {
    let resData = await reqEmptyUrlInfo();
    commit("EMPTYURLINFO", { resData });
  },
  //获取测试用例列表-json文件
  async queryTestItemList({ commit }) {
    let resData = await reqTestItemInfo();
    commit("QUERYTESTITEMLIST", { resData });
  },
  //开始测试用例
  async startTestUrlInfo({ commit }) {
    let resData = await reqStartTest();
    commit("STARTTESTURLINFO", { resData });
  },
  //获取测试结果列表-json文件
  async queryTestResultList({ commit }) {
    let resData = await reqTestResultInfo();
    commit("QUERYTESTRESULTLIST", { resData });
  },

  //获取厂商任务列表
  async queryTaskInfo({ commit }, obj) { //obj:{params:xxx, filterStartDateFlag:xxx}
    let resData = await reqTaskInfo(obj.params);
    if(resData.code === 400){
      return;
    }
    // console.log('obj.filterStartDateFlag:',obj.filterStartDateFlag);
    let filteredData = resData.task;
    if (obj.filterStartDateFlag) {
      filteredData = resData.task.filter((item)=>{
        let {start_date} = item;
        return start_date !=="-1"
      })
    }
    const data = filteredData.map((item,index)=>{
      let {task_id,start_date,stop_date,cameraNums} = item;
      start_date = start_date==="-1"?"--":dayjs(start_date).format('YYYY/MM/DD HH:mm:ss');
      stop_date = stop_date==="-1"?"--":dayjs(stop_date).format('YYYY/MM/DD HH:mm:ss');
      return{
        id:index+1,
        task_id:task_id,
        cameraNums:cameraNums.split(","),
        start_date:start_date,
        stop_date:stop_date
      }
    })
    commit("QUERYTASKINFO",{data})
  },

  //获取稳定性测试结果
  async queryMonitorResultInfo({commit},params){
    const resData = await reqMonitorResultInfo(params);
    commit("QUERYMONITORRESULTINFO",{resData})
  },

  //修改分页total总数
  changeItemTotal({ commit }, total) {
    commit("CHANGEITEMTOTAL", { total });
  },

  async queryCameraStatus({commit},params){
    const resData = await reqCameraStatus(params);
    commit("QUERYCAMERASTATUS",{resData});
  },

  queryPushStream({commit},params){
    return new Promise(async (resolve,reject)=>{
      const resData = await reqPushStream(params.formParams,params.company);
      if(resData.code === 500){
        reject(resData.code)
      }
      else{
        resolve()
      }
    })
  },

  async queryStopStream({commit},params){
    const resData = await reqStopStream(params.formParams,params.company);
  },

  //获取厂商已有信息(信令-开始推流)
  async getDeviceUsersDefinitelyInfo({commit},companyName) {
    let param = { username: companyName };
    let resData = await reqDeviceUsersInfo(param);
    commit("GETDEVICEUSERSDEFINITELYINFO",{resData})
  },
};
